"use strict";

var STORAGE_TYPE = Object.freeze({
    LOCAL: "local",
    SYNC: "sync"
}), myStorage = {
    __storageType: STORAGE_TYPE.LOCAL,
    isSynced: function() {
        return myStorage.__storageType === STORAGE_TYPE.SYNC;
    },
    init: function(callback, callbackError) {
        myStorage.getDataAll(callback, callbackError, 3, !0);
    },
    getDataAll: function(callback, callbackError, retryOnErrorCount, withConvert) {
        var errorCount = 0;
        function errorHandler(error) {
            window.console.error(error);
            ++errorCount <= retryOnErrorCount ? setTimeout(run, 0) : callbackError && callbackError(error);
        }
        function run() {
            $.storage.get(STORAGE_TYPE.LOCAL, null, function(data) {
                withConvert && localStorage.verzija && (data = myStorage.convertLocalStorageToStorageAPI());
                if (data.__storageType === STORAGE_TYPE.SYNC) {
                    myStorage.__storageType = STORAGE_TYPE.SYNC;
                    $.storage.get(myStorage.__storageType, null, callback, errorHandler);
                } else {
                    myStorage.__storageType = STORAGE_TYPE.LOCAL;
                    callback(data);
                }
            }, errorHandler);
        }
        run();
    },
    convertLocalStorageToStorageAPI: function() {
        function remap(data, oldKeys, currentMap, mapType) {
            for (var key in currentMap) if (Object.prototype.hasOwnProperty.call(currentMap, key) && void 0 !== localStorage[key]) {
                oldKeys.push(key);
                var tempValue = localStorage[key];
                switch (mapType) {
                  case "boolean":
                    if ("true" === tempValue) tempValue = !0; else {
                        if ("false" !== tempValue) continue;
                        tempValue = !1;
                    }
                    break;

                  case "integer":
                    tempValue = Number(tempValue);
                    if (Number.isNaN(tempValue)) continue;
                    break;

                  case "object":
                    if ("string" != typeof tempValue || "" === tempValue || "[]" === tempValue) continue;
                    tempValue = JSON.parse(tempValue);
                }
                data[currentMap[key]] = tempValue;
            }
        }
        var oldKeys = [], dataNew = {
            __storageType: STORAGE_TYPE.LOCAL
        };
        remap(dataNew, oldKeys, {
            popup_menu_close: "showPopupMenuClose",
            show_suggestions: "showSuggestions",
            website_regex: "websiteRegex"
        }, "boolean");
        remap(dataNew, oldKeys, {
            website_list: "websiteList"
        }, "object");
        remap(dataNew, oldKeys, {
            verzija: "version"
        }, "string");
        $.storage.set(dataNew.__storageType, dataNew, function() {
            for (var i = 0; i < oldKeys.length; i++) void 0 !== localStorage[oldKeys[i]] && localStorage.removeItem(oldKeys[i]);
        }, function(error) {
            window.console.error("Error: " + error);
        });
        void 0 !== localStorage.popup_menu_hide_icon && localStorage.removeItem("popup_menu_hide_icon");
        void 0 !== localStorage.show_icon && localStorage.removeItem("show_icon");
        void 0 !== localStorage.show_icon_enabled && localStorage.removeItem("show_icon_enabled");
        return dataNew;
    }
};